package com.smartum.mpr.dao;


import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.exception.AlreadyExistsUserException;
import com.smartum.mpr.exception.AuthorizationException;
import com.smartum.mpr.exception.DatabaseIntegrityException;
import com.smartum.mpr.exception.NotExistsTokenException;
import com.smartum.mpr.model.User;

@Repository
public class UserDao extends GenericDao<Long, User> {

	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}
    
	
	
	public User findUserByToken(String token) throws DatabaseIntegrityException, NotExistsTokenException {
		System.out.println("Llego findUserByToken");
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.createCriteria("token").add(Restrictions.eq("token", token));
		System.out.println("obtubo token");

		List<User> users = hibernateTemplate.findByCriteria(criteria);
		System.out.println("obtubo users:" + users.size());
		if (users.size() == 0) {
			throw new NotExistsTokenException();
		} else if (users.size() > 1) {
			throw new DatabaseIntegrityException("database has few equals tokens: " + token);
		} else if (users.size() == 1) {
			return users.get(0);
		} else {
			throw new DatabaseIntegrityException();
		}
	}

	public User findUserByLoginAndPassword(String login, String password) throws DatabaseIntegrityException,
	        AuthorizationException {
		DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
		criteria.add(Restrictions.eq("username", login));
		criteria.add(Restrictions.eq("password", password));
		List<User> users = hibernateTemplate.findByCriteria(criteria);
		if (users.size() > 1) {
			throw new DatabaseIntegrityException("database has more than one equals users with login: " + login);
		} else if (users.size() == 0) {
			throw new AuthorizationException("user not found");
		} else if (users.size() == 1) {
			return users.get(0);
		} else {
			throw new DatabaseIntegrityException();
		}
	}

	public void searchRegistrationUserLoginAndEmail(String login, String email) throws AlreadyExistsUserException {
		DetachedCriteria criteriaByUsername = DetachedCriteria.forClass(getEntityClass());
		criteriaByUsername.add(Restrictions.eq("username", login));

		List<User> usersWithRegistrationLogin = hibernateTemplate.findByCriteria(criteriaByUsername);
		if (usersWithRegistrationLogin.size() > 0) {
			throw new AlreadyExistsUserException(String.format("user with login: %s  already exists", login));
		}

		DetachedCriteria criteriaByEmail = DetachedCriteria.forClass(getEntityClass());
		criteriaByEmail.add(Restrictions.eq("email", email));

		List<User> usersWithRegistrationEmail = hibernateTemplate.findByCriteria(criteriaByEmail);
		if (usersWithRegistrationEmail.size() > 0) {
			throw new AlreadyExistsUserException(String.format("user with email: %s  already exists", email));
		}

	}

}
